package com.nhjf.dao.coursesPlayback;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.coursesPlayback.CoursesPlayback;
import com.nhjf.model.teachers.Courses;
import com.nhjf.pojo.qo.coursesPlayback.CoursesPlaybackQo;
import com.nhjf.pojo.qo.teachers.CoursesQo;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class CoursesPlaybackDao extends BaseDao<CoursesPlayback,CoursesPlaybackQo> {
    /**
     * 课程显示
     */


    @Override
    protected Criteria buildCriteria(Criteria criteria, CoursesPlaybackQo qo) {
        if(qo!=null){

//            criteria.setFetchMode("resourceList", FetchMode.SELECT);
        	criteria.setFetchMode("uploader", qo.isSeachTeacher()?FetchMode.JOIN:FetchMode.SELECT);	
            //id
            if(qo.getCoursesPlaybackId()!=null){
                criteria.add(Restrictions.eq("id",qo.getCoursesPlaybackId()));
            }
            //课程标题
            if(StringUtils.isNotBlank(qo.getCoursetitle())){
                criteria.add(Restrictions.like("coursetitle",qo.getCoursetitle(),MatchMode.ANYWHERE));
            }
            if(qo.getCourseTypeOne()!=null) {
            	criteria.add(Restrictions.eq("courseTypeOneId",qo.getCourseTypeOne()));
            }
            if(qo.getCourseTypeTwo()!=null) {
            	criteria.add(Restrictions.eq("courseTypeTwoId",qo.getCourseTypeTwo()));
            }
            //课程副标题
            if(StringUtils.isNotBlank(qo.getCoursetitleTwo())){
                criteria.add(Restrictions.like("coursetitleTwo",qo.getCoursetitleTwo(),MatchMode.ANYWHERE));
            }

            //讲师
            if(StringUtils.isNotBlank(qo.getTeacher())){
                criteria.add(Restrictions.like("teacher",qo.getTeacher(),MatchMode.ANYWHERE));
            }

            if(qo.getTeacherId()!=null) {
				criteria.add(Restrictions.eq("uploader.id",qo.getTeacherId()));
            }
            //是否付费
            if(qo.getIsPay()!=null) {
            	criteria.add(Restrictions.like("isPay", qo.getIsPay()));
            }
            //发布时间
            if(qo.getReleasetime()!=null){
                criteria.add(Restrictions.eq("releasetime",qo.getReleasetime()));
            }
            
            if(qo.getOrganizationId()!=null) {
				criteria.createAlias("uploader", "uploader");
				criteria.createAlias("uploader.traininginstitution", "uploader.traininginstitution");
				criteria.add(Restrictions.eq("uploader.traininginstitution.id",qo.getOrganizationId()));
            	
            }
            if(qo.getCourseShares()!=null) {
            	 criteria.add(Restrictions.ne("courseShare",qo.getCourseShares()));
            }
            //视频地址
            if(StringUtils.isNotBlank(qo.getVideo())){
                criteria.add(Restrictions.eq("video",qo.getVideo()));
            }
            criteria.addOrder(Order.asc("releasetime"));
        }
        return criteria;
    }


    @Override
    protected Class<CoursesPlayback> getEntityClass() {
        return CoursesPlayback.class;
    }
}
